package com.smugmugsoftware.airconcalc.advanced;

import android.os.Parcel;
import android.os.Parcelable;

public class SensibleLoad extends RoomComponent {
	
	public static final double BTU_CONVERSION_FACTOR = 3.412142;
	
	public static final int LIGHTING = 3000;
	public static final int LIGHT_BULB = 3001;
	public static final int LIGHT_CFL = 3002;
	public static final int LIGHT_LED = 3003;

	public static final int OCCUPANT = 4000;

	public static final int APPLIANCE = 5000;
	public static final int APPLIANCE_KITCHEN_BTU = 5001;
	public static final int APPLIANCE_KITCHEN_WATT = 5002;
	public static final int APPLIANCE_FIREPLACE = 5003;
	
	public static final int COMMERCIAL = 6000;
	public static final int COMMERCIAL_CUSTOM_BTU = 6001;
	public static final int COMMERCIAL_CUSTOM_WATT = 6002;
	
	private int wattage;
	private double efficiency;
	private int type;
	private int btu;
	private int qty;
	
	private String name;
	
	private boolean usesBTU;
	private boolean usesWatts;
	
	public SensibleLoad(long id, int type) {
		super(id);
		this.type = type;
		this.usesBTU = false;
		this.usesWatts = true;
		
		initializeValues();
	}
	
	public SensibleLoad(int id, int type, String constNo) {
		super(id);
		this.type = type;
		this.constructionNumber = constNo;
		this.usesBTU = true;
		this.usesWatts = false;
		
		initializeValues();
	}

	private void initializeValues() {
		name = "";
		wattage = 0;
		efficiency = 0;
		btu = 0;
		qty = 0;
	}
	
	public String getLoadDescriptorString() {
		if (usesWatts && type != OCCUPANT) {
			return (String.valueOf(wattage) + " Watts");
		} else {
			return "";
		}
	}
	
	public void setWatts(int watts, int efficiency) {
		this.wattage = watts;
		this.efficiency = (double) ((double) efficiency / 100);
		this.usesWatts = true;
		this.usesBTU = false;
	}
	
	public void setBTU(int BTU) {
		this.btu = BTU;
		this.usesBTU = true;
		this.usesWatts = false;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setConstNo(String constNo) {
		this.constructionNumber = constNo;
	}

	public int getType() {
		return type;
	}
	
	public String getTypeString() {

		if (type == LIGHT_BULB) {
			return "(Incandescent)";
		} else if (type == LIGHT_CFL) {
			return "(Flourescent)";
		} else if (type == LIGHT_LED) {
			return "(LED Bulb)";
		}
		return "(" + name + ")";
	}
	
	public String getName() {
		if (type == LIGHT_BULB) {
			return "Incandescent";
		} else if (type == LIGHT_CFL) {
			return "Flourescent";
		} else if (type == LIGHT_LED) {
			return "LED Bulb";
		}
		return name;
	}
	
	@Override
	public String getConstructionNumber() {
		if (type == LIGHT_BULB) {
			return "L-I" + wattage;
		} else if (type == LIGHT_CFL) {
			return "L-C" + wattage;
		} else if (type == LIGHT_LED) {
			return "L-L" + wattage;
		} else {
			return super.getConstructionNumber();
		}
	}

	public int getBtu() {
		if (usesBTU) {
			return (int) ((double) btu);
		} else {
			return (int) ((double) wattage * (double) efficiency * (double) BTU_CONVERSION_FACTOR);
		}
	}
	
	public int getTotalBtu() {
		if (usesBTU) {
			return (int) ((double) btu * (double) qty);
		} else {
			return (int) ((double) wattage * (double) efficiency * (double) qty * (double) BTU_CONVERSION_FACTOR);
		}
	}
	
	public boolean usesBTU() {
		return usesBTU;
	}

	public boolean usesWatts() {
		return usesWatts;
	}

	public void setQuantity(int qty) {
		this.qty = qty;
	}
	
	public int getQty() {
		return qty;
	}
	
	public void writeToParcel(Parcel dest, int flags) {
		super.writeToParcel(dest, flags);

		dest.writeString(name);
		dest.writeDouble(efficiency);
		dest.writeInt(wattage);
		dest.writeInt(type);
		dest.writeInt(btu);
		dest.writeInt(qty);
		dest.writeBooleanArray(new boolean[] { usesBTU, usesWatts });
	}

	public static final Parcelable.Creator<SensibleLoad> CREATOR = new Parcelable.Creator<SensibleLoad>() {
		public SensibleLoad createFromParcel(Parcel in) {
			return new SensibleLoad(in);
		}

		public SensibleLoad[] newArray(int size) {
			return new SensibleLoad[size];
		}
	};

	public SensibleLoad(Parcel p) {
		super(p);
		
		name = p.readString();
		efficiency = p.readDouble();
		wattage = p.readInt();
		type = p.readInt();
		btu = p.readInt();
		qty = p.readInt();
		
		boolean[] newBooleanArray = new boolean[2];
		p.readBooleanArray(newBooleanArray);
		usesBTU = newBooleanArray[0];
		usesWatts = newBooleanArray[1];
	}
}
